package com.ruoyi.util.http.cases;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * @Description: put和delete请求的例子 
 * @ClassName: PutAndDelete
 * @author syc
 * @date: 2022年8月3日 上午8:49:30
 * 	请注意 httpPut是Content-type:application/json 的数据传输
 *  httpPut2是 Content-type:application/x-www-form-urlencoded 的数据传输
 */
public class PutAndDelete {
	
	/**
	 * @Description: 常规的 
	 * @author syc
	 * @date: 2022年8月3日 上午9:03:26
	 */
	public static String httpPut(String url, Map<String, String> headerMap, JSONObject jsonParam,
			String charset) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse = null;
		try {
			if (StringUtils.isBlank(charset)) {
				charset = "UTF-8";
			}		
			HttpPut httpPut = new HttpPut(url);
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)
					.setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
			httpPut.setConfig(requestConfig);
			/**
			 * 设置头信息
			 */
			if (headerMap != null && headerMap.size() > 0) {
				for (String name : headerMap.keySet()) {
					httpPut.addHeader(name, headerMap.get(name));
				}
			}
			StringEntity entityParam = new StringEntity(jsonParam.toString(), "utf-8");
			entityParam.setContentEncoding("UTF-8");
			entityParam.setContentType("application/json");
			httpPut.setEntity(entityParam);
			httpResponse = httpClient.execute(httpPut);
			HttpEntity entity = httpResponse.getEntity();
			String result = EntityUtils.toString(entity);
			return result;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (httpResponse != null) {
				try {
					httpResponse.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	
	/**
	 * @Description:为Content-type:application/x-www-form-urlencoded专用的
	 * @author syc
	 * @date: 2022年8月3日 上午9:03:56
	 */
	public static String httpPut2(String url, Map<String, String> headerMap, Map<String, String> paramMap,
			String charset) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse = null;
		try {
			if (StringUtils.isBlank(charset)) {
				charset = "UTF-8";
			}
			HttpPut httpPut = new HttpPut(url);
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)
					.setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
			httpPut.setConfig(requestConfig);
			/**
			 * 设置头信息
			 */
			if (headerMap != null && headerMap.size() > 0) {
				for (String name : headerMap.keySet()) {
					httpPut.addHeader(name, headerMap.get(name));
				}
			}
			/**
			 * 设置参数
			 */
			StringBuilder stringBuilder = new StringBuilder();
			if (paramMap != null && paramMap.size() > 0) {
				for (String key : paramMap.keySet()) {
					String value = paramMap.get(key);
					if (StringUtils.isNotBlank(value)) {
						if (stringBuilder.length() > 0) {
							stringBuilder.append("&");
						}
						stringBuilder.append(key).append("=").append(URLEncoder.encode(value, charset));
					}
				}
			}
			StringEntity entityParam = new StringEntity(stringBuilder.toString(), "utf-8");
			entityParam.setContentEncoding("UTF-8");
			entityParam.setContentType("application/json");
			httpPut.setEntity(entityParam);
			httpResponse = httpClient.execute(httpPut);
			HttpEntity entity = httpResponse.getEntity();
			String result = EntityUtils.toString(entity);
			return result;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (httpResponse != null) {
				try {
					httpResponse.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	/**
	 * @Description: 一个普普通通的delete请求
	 * @author syc
	 * @date: 2022年8月3日 上午9:05:04
	 */
	public static String httpdelete(String url, Map<String, String> headerMap) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse = null;
		try {	
			HttpDelete httpDelete = new HttpDelete(url);
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)
					.setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
			httpDelete.setConfig(requestConfig);
			/**
			 * 设置头信息
			 */
			if (headerMap != null && headerMap.size() > 0) {
				for (String name : headerMap.keySet()) {
					httpDelete.addHeader(name, headerMap.get(name));
				}
			}
			httpResponse = httpClient.execute(httpDelete);
			HttpEntity entity = httpResponse.getEntity();
			String result = EntityUtils.toString(entity);
			return result;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (httpResponse != null) {
				try {
					httpResponse.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
    
//    public static void main(String[] args) {
//    	String token = "Bearer YWMtXJeYvBF6Ee2R8BEsRog4dNNfR6WNGjm-soJ15ogGa5LRMLA2LTlN65sCpkBUyxfSAgMAAAGCWK-KmwAPoABOssGvzMvfLRY5KPFEF1lJufWw4Q84hYvBSZ86HKHFGA";
//		// 一.对httpPut进行测试 (修改环信用户昵称)
////    	String url = "http://a1.easemob.com/1123220407104638/spth/users/szjk751503781442752512";
////    	Map<String, String> headerMap = new HashMap<String, String>();
////		JSONObject jsonParam = new JSONObject();
////		headerMap.put("Content-type", "application/json");
////		headerMap.put("Authorization", token);
////		jsonParam.put("nickname", "王小二");
////		String ticket = PutAndDelete.httpPut(url, headerMap, jsonParam, null);
////    	if(StringUtils.isNotBlank(ticket)) {
////    		JSONObject jsonObjectResult = JSONObject.parseObject(ticket);
////    		System.out.println(jsonObjectResult.toString());
////    	}
//    	
//    	// 二.对httpPut2进行测试(修改环信用户属性)
////    	String url = "http://a1.easemob.com/1123220407104638/spth/metadata/user/szjk751503781442752512";
////    	Map<String, String> headerMap = new HashMap<String, String>();
////		Map<String, String> paramMap = new HashMap<String, String>();
////		headerMap.put("Content-type", "application/x-www-form-urlencoded");
////		headerMap.put("Authorization", token);
////		paramMap.put("nickname", "王二小");
////		paramMap.put("avatarurl", "http://jdsdkdjk/234.png");
////		String ticket = PutAndDelete.httpPut2(url, headerMap, paramMap, null);
////		if(StringUtils.isNotBlank(ticket)) {
////			JSONObject jsonObjectResult = JSONObject.parseObject(ticket);
////			System.out.println(jsonObjectResult.toString());
////		}
//	}
	
}
